import { quest } from "../../utils/http";

// pages/userInfo/userInfo.js
Page({
  /**
   * 页面的初始数据
   */
  data: {
    content: {},
    genderList: [
      { id: 0, name: "男" },
      { id: 1, name: "女" },
      { id: 2, name: "保密" },
    ],
    isBanned: false,
    university: [],
    myUniversity: {},
    myGender: {},
    isAdmin: false,
  },
  // item={"id":13,"openId":"081KREFa1sdkJC0qQrGa1dKXtw4KREFF","avatar":"https://ts1.cn.mm.bing.net/th
  // item={"id":44,"openId":"oEYhX487tlX4yyIM5LpuxFRsj23o","avatar":"https://thirdwx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTL40CYbBtPXN30wSo3oCACE55mACicibp5EJq25HjCaR0YBXW93d9l5hrSH425ZMd3KJGvEHYj1t3YA/132","phone":"12345678911","nickName":"°One","gender":0,"universityId":1,"ban":1,"grade":5,"extendId":null}
  // item={"id":13,"openId":"081KREFa1sdkJC0qQrGa1dKXtw4KREFF","avatar":"https://ts1.cn.mm.bing.net/th

  async onLoad(e) {
    let content = JSON.parse(e.item);

    let myUniversity;
    let myGender;
    let isBanned = false;
    let isAdmin = false;
    if (content.grade < 5) {
      isAdmin = false;
    } else {
      isAdmin = true;
    }
    if (content.ban == 1) {
      isBanned = false;
    } else {
      isBanned = true;
    }
    let university = await quest({
      url: "university/list?size=99",
    });
    console.log(university);
    university.data.items.forEach((element) => {
      if (element.id == content.universityId) {
        myUniversity = element;
      }
    });
    this.data.genderList.forEach((i) => {
      if (i.id == content.gender) {
        myGender = i;
      }
    });
    this.setData({
      isAdmin,
      isBanned,
      myGender,
      content,
      myUniversity,
      university: university.data.items,
    });
  },
  bindUniversityChange(e) {
    let { university } = this.data;
    let myUniversity = university[e.detail.value];
    this.setData({
      myUniversity,
    });
  },
  bindGenderChange: function (e) {
    let { genderList } = this.data;
    let myGender = genderList[e.detail.value];
    this.setData({
      myGender,
    });
  },
  bindBannedChange: function (e) {
    this.setData({
      isBanned: e.detail.value,
    });
  },
  bindAdminChange: function (e) {
    this.setData({
      isAdmin: e.detail.value,
    });
  },
  onSubmit: function () {
    // 提交表单数据
  },
  onReset: function () {
    this.setData({
      genderIndex: 0,
      isBanned: false,
    });
  },
  async onSubmit(e) {
    let { content, isAdmin, isBanned, myGender, myUniversity } = this.data;
    content.ban = isBanned ? 0 : 1;
    content.universityId = myUniversity.id;
    content.gender = myGender.id;
    if (isAdmin) {
      content.grade = 5;
    } else {
      content.grade = 1;
    }
    console.log(content.grade);
    let data = await quest({
      url: "user/update",
      method: "post",
      data: {
        ...content,
      },
    });
    if (data.code == 200) {
      console.log(data);
      wx.showModal({
        cancelText: "取消",
        confirmText: "确定",
        content: "修改成功",
        editable: false,
        showCancel: true,
        title: "提示",
        success: (result) => {},
        fail: (res) => {},
        complete: (res) => {},
      });
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {},
});
